package com.sl.au.interceptor;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.history.HistoricVariableInstance;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.sl.au.entity.JPushMsg;
import com.sl.au.service.ActivitiService;
import com.sl.au.service.JGPushService;
import com.sl.au.service.UserService;

@Component
public class ActivitiTaskListener implements ApplicationContextAware, ExecutionListener, TaskListener {

	private ProcessEngine processEngine;

	private JGPushService jPushService;

	private static ApplicationContext cxt;
	private static UserService userService;
	private static ActivitiService activitiService;

	public ActivitiTaskListener() {

		this.jPushService = new JGPushService();
		this.processEngine = ProcessEngines.getDefaultProcessEngine();

	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void notify(DelegateTask delegateTask) {
		String name = delegateTask.getName();
		String s = delegateTask.getAssignee();
		String taskId = delegateTask.getId();
		String pId = delegateTask.getProcessInstanceId();
		if (null == this.processEngine)
			this.processEngine = ProcessEngines.getDefaultProcessEngine();
		List<HistoricVariableInstance> hlist = processEngine.getHistoryService()//
				.createHistoricVariableInstanceQuery()// 创建一个历史的流程变量查询对象
				.processInstanceId(pId)//
				.list();
		String entityId = "", entityName = "", contractType = "", starter = "";
		String auditAdvice = "", auditorNumber = "", auditState = "";
		String processDefinitionName = "", content = "", auditor = "";
		String projectName = "", unitName = "";
		if (hlist != null && hlist.size() > 0) {
			for (HistoricVariableInstance hvi : hlist) {
				switch (hvi.getVariableName().toString()) {
				case "entityId":
					entityId = hvi.getValue().toString();
					break;
				case "entityName":
					entityName = hvi.getValue().toString().toString();
					break;
				case "processDefinitionName":
					processDefinitionName = hvi.getValue().toString().toString();
					break;
				case "contractType":
					contractType = hvi.getValue().toString().toString();
					break;
				case "auditAdvice":
					auditAdvice = hvi.getValue().toString().toString();
					break;
				case "auditorNumber":
					auditorNumber = hvi.getValue().toString().toString();
					break;
				case "auditState":
					auditState = hvi.getValue().toString().toString();
					break;
				case "starter":
					starter = hvi.getValue().toString().toString();

				}

			}
		}
		if (StringUtils.hasText(s)) {// 上级通过，向下级发消息
			String l = "";
			switch (name.substring(0, 1)) {
			case "二":
				l = "1";
				break;
			case "三":
				l = "2";
				break;
			case "四":
				l = "3";
				break;
			case "五":
				l = "4";
				break;
			case "六":
				l = "5";
				break;
			case "七":
				l = "6";
				break;
			case "八":
				l = "7";
				break;

			}
			if (StringUtils.hasText(l))
				auditor = delegateTask.getVariable("auditor" + l).toString();
			String starterName = userService.getRepository().getOne(s).getName();
			String auditorName = "";
			if (StringUtils.hasText(auditor))
				auditorName = userService.getRepository().getOne(auditor).getName();
			JPushMsg msg = new JPushMsg();
			List<String> list = new ArrayList<>();
			list.add(s.replaceAll("-", ""));
			msg.setAlias(list);
			if (name.startsWith("一"))
				content = processDefinitionName + ":来自" + starterName + "审批提交。";
			else
				content = processDefinitionName + ":来自" + auditorName + "通过的审批事项。";
			msg.setContent(content);
			msg.setTitle("审批通知");
			jPushService.sendClient(msg, null);
			return;
		}

	}

	@Override
	public void notify(DelegateExecution execution) throws Exception {

		String event = execution.getEventName();
		if ("start".equals(event)) {
			String projectName = "", content = "";
			String auditState = execution.getVariable("auditState").toString();
			String processDefinitionName = execution.getVariable("processDefinitionName").toString();
			String entityId = execution.getVariable("entityId").toString();
			String starter = execution.getVariable("starter").toString();

			if (StringUtils.hasText(entityId) && StringUtils.hasText(processDefinitionName))
				projectName = this.activitiService.getTaskTitle(entityId, processDefinitionName);

			JPushMsg msg = new JPushMsg();
			List<String> list = new ArrayList<>();
			list.add(starter.replaceAll("-", ""));
			if ("通过".equals(auditState)) {
				msg.setAlias(list);
				content = processDefinitionName + ":" + projectName + "已通过审批。";

				msg.setContent(content);
				msg.setTitle("审批通知");
				jPushService.sendClient(msg, null);
			} else if ("驳回".equals(auditState)) {
				msg.setAlias(list);
				content = processDefinitionName + ":" + projectName + "已被驳回。";

				msg.setContent(content);
				msg.setTitle("审批通知");
				jPushService.sendClient(msg, null);

			}

		}

	}

	@Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		this.cxt = arg0;
		if (null != cxt) {
			this.activitiService = (ActivitiService) cxt.getBean("activitiService");
			this.userService = (UserService) cxt.getBean("userService");
		}
	}

}
